package com.hdd.demo.controller;

import com.hdd.demo.entity.User;
import com.hdd.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    //查询全部
    @GetMapping("/index.html")
    public String index(Map<String, List<User>> map){
        List<User> list = userService.getAll();
        map.put("users",list);
        return "index";
    }

    //添加
    @PostMapping("/add")
    public String addAccount(User user){
        userService.add(user);
        return "redirect:/index.html";
    }
    //删除数据,本来需要使用DeleteMapping，但是由于没有界面可以隐藏，所以这里就直接写了RequestMapping
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpServletResponse servletResponse) throws IOException {
        userService.delete(id);
        System.out.println("delete方法执行");
        return "redirect:/index.html";
    }
    //根据id进行修改
    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model, @PathVariable int id){
        User users = userService.get(id);
        model.addAttribute("users",users);
        //表示跳转到modifie,html界面
        return "modifie";
    }
    @PutMapping("/update")
    public String updateUser(Model model, User user, HttpServletRequest request) {
        String id = request.getParameter("id");
        User userById = userService.get(Integer.parseInt(id));
        userService.update(user);
        System.out.println(user);
        return "redirect:/index.html";
    }

}
